package com.example.weixinsystem.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.weixinsystem.service.SysMenuService;
import com.example.weixinsystem.service.SysRoleService;
import com.example.weixinsystem.service.SysUserService;
import com.example.weixinsystem.mapper.SysUserMapper;
import com.weixin.common.core.constant.Constants;
import com.weixin.common.core.domain.LoginUser;
import com.weixin.common.core.entity.SysUser;
import com.weixin.common.core.exception.BaseException;
import com.weixin.common.core.form.LoginForm;
import com.weixin.common.core.form.UserAddForm;
import com.weixin.common.core.utils.PwdUtil;
import com.weixin.common.core.utils.SecurityUtil;
import com.weixin.common.core.utils.SpringBeanUtil;
import com.weixin.common.redis.service.RedisService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author zhang
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    private RedisService redisService;

    public SysUserServiceImpl(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * 根据用户名称查询用户
     *
     * @param username 用户名
     * @return
     */
    @Override
    public SysUser getUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUserName, username);
        wrapper.eq(SysUser::getDelFlag, Constants.NOT_DEL_FLAG);
        return this.baseMapper.selectOne(wrapper);
    }

    /**
     * 根据用户名获取当前登录用户基础信息
     *
     * @param username 用户名
     * @return
     */
    @Override
    public LoginUser getUserInfo(String username) {
        LoginUser loginUser = new LoginUser();
        SysUser sysUser = getUserByUsername(username);
        if (ObjectUtils.isEmpty(sysUser)) {
            throw new BaseException("用户名或密码错误!");
        }
        loginUser.setSysUser(sysUser);
        loginUser.setUserId(sysUser.getId());
        loginUser.setUsername(sysUser.getUserName());

        Set<String> permissions = SpringBeanUtil.getBean(SysMenuService.class).getUserPossessMenu(sysUser.getId());
        loginUser.setPermissions(permissions);

        Set<String> roles = SpringBeanUtil.getBean(SysRoleService.class).getUserPossessRole(sysUser.getId());
        loginUser.setRoles(roles);
        return loginUser;
    }

    /**
     * 用户注册
     *
     * @param addParam 用户注册参数
     */
    @Override
    public void addUser(LoginForm addParam) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName(addParam.getUserName());
        sysUser.setPassword(SecurityUtil.encryptPassword(addParam.getPassword()));
        sysUser.setCreatedBy(addParam.getUserName());
        sysUser.setCreatedTime(DateUtil.date());
        sysUser.setDelFlag(Constants.NOT_DEL_FLAG);
        this.save(sysUser);
    }
}
